package com.wufun.overseas.main.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.papa.controller.core.PadKeyEvent;
import com.wufun.overseas.R;
import com.wufun.overseas.joystick.map.BlackListManager;
import com.wufun.overseas.joystick.map.KeyCodes;
import com.wufun.overseas.joystick.map.KeyMap;
import com.wufun.overseas.joystick.map.KeyMapManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//FBA设置
public class HandShankFBAActivity extends HandShankBaseActivity implements OnClickListener {

    //region Views
    @Bind(R.id.select)
    Button select;
    @Bind(R.id.start)
    Button start;

    @Bind(R.id.a)
    Button a;
    @Bind(R.id.b)
    Button b;
    @Bind(R.id.c)
    Button c;
    @Bind(R.id.d)
    Button d;
    @Bind(R.id.e)
    Button e;
    @Bind(R.id.f)
    Button f;
    @Bind(R.id.ab)
    Button ab;
    @Bind(R.id.cd)
    Button cd;
    @Bind(R.id.abc)
    Button abc;
    @Bind(R.id.f1)
    Button f1;
    @Bind(R.id.f2)
    Button f2;
    @Bind(R.id.f3)
    Button f3;
    @Bind(R.id.f4)
    Button f4;
    @Bind(R.id.sp1)
    Button sp1;
    @Bind(R.id.sp2)
    Button sp2;
    @Bind(R.id.sp3)
    Button sp3;
    @Bind(R.id.sp4)
    Button sp4;
    @Bind(R.id.sp5)
    Button sp5;


    @Bind(R.id.titleText)
    TextView titleText;
    @Bind(R.id.resetButn)
    Button   resetButn;
    //endregion

    private boolean isReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fba_key_map1);
        ButterKnife.bind(this);

        afterViews();
    }

    @OnClick(R.id.backImage)
    void backImage() {
        finish();
    }

    @OnClick(R.id.okButn)
    void okButn() {

        if (isReset) {
            isReset = false;
            finish();
            return;
        } else {
            //新版映射数多于按键数，不在检查映射数量，即映射可以为空
//            if (okMap.size() < 11) {
//                unSet();
//            }

//            KeyMapManager.writeToConfig(this, KeyMap.EmuMap.FBA, putMap(okMap));
            keyMapManager.writeToConfig(this, emuMap, emuMapKeysTemp);
//            keyMapManager.writeToConfigVK(this, emuMap, emuMapKeysTemp);//兼容旧版
            finish();
        }


    }

    @OnClick(R.id.resetButn)
    void resetButn() {
        //KeyMapManager.writeToConfig(KeyMap.EmuMap.FBA, HandShankUtil.fbaMap);
//        reset();

        emuMapKeysTemp.clear();
        Iterator<String> iterator = emuMapKeys.keySet().iterator();
        while (iterator.hasNext()) {
            String s   = iterator.next();
            String val = "";
            emuMapKeys.put(s, val);
        }
        if (emuMap != null) {
            iterator = emuMap.map.keySet().iterator();
            while (iterator.hasNext()) {
                String s   = iterator.next();
                String val = emuMap.map.get(s);
                emuMapKeys.put(s, val);
            }
        }


        emuMapKeysTemp.putAll(emuMapKeys);
        bindViews(views, items);
    }

    String title = "";
    KeyMap.EmuMap emuMap = KeyMap.EmuMap.FBA;
    View[] views;
    Item[] items;

    KeyMapManager            keyMapManager = new KeyMapManager(emuMap);
    HashMap<Integer, String> customKeys    = new HashMap<>();//<sys_key, custom_key>
    HashMap<String, String>  emuMapKeys    = new HashMap<>();//<emu_key, custom_key>

    HashMap<String, String> emuMapKeysTemp = new HashMap<>();

    //    @AfterViews
    void afterViews() {
        title = getString(R.string.gamepad_fba_settings);
        titleText.setText(title);
        initEmuMap();

//        initMap();
//        initBtn();
//        initView();

        initView();
    }

    private void initEmuMap() {
        keyMapManager.load(emuMap);
        Map<String, Integer>                 map      = keyMapManager.getMap(KeyMap.keySection);
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            customKeys.put(entry.getValue(), entry.getKey());
        }

        emuMapKeys.putAll(keyMapManager.getMap(emuMap.section));
        emuMapKeysTemp.putAll(emuMapKeys);
    }

    private void initView() {
        views = new View[]{
                select, start,
                abc, ab, cd,
                a, b, c, d, e, f,
                f1, f2, f3, f4,
                sp1, sp2, sp3, sp4, sp5
        };
        items = new Item[]{
                new Item(KeyCodes.KEY_SELECT), new Item(KeyCodes.KEY_SELECT)
                , new Item(KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value() | KeyCodes.KEY_C.value()), new Item(KeyCodes.KEY_A.value() | KeyCodes.KEY_B.value()), new Item(KeyCodes.KEY_C.value() | KeyCodes.KEY_D.value())
                , new Item(KeyCodes.KEY_A), new Item(KeyCodes.KEY_B), new Item(KeyCodes.KEY_C), new Item(KeyCodes.KEY_D), new Item(KeyCodes.KEY_E), new Item(KeyCodes.KEY_F)
                , new Item(KeyCodes.KEY_1), new Item(KeyCodes.KEY_2), new Item(KeyCodes.KEY_3), new Item(KeyCodes.KEY_4)
                , new Item(KeyCodes.KEY_SP0), new Item(KeyCodes.KEY_SP1), new Item(KeyCodes.KEY_SP2), new Item(KeyCodes.KEY_SP3), new Item(KeyCodes.KEY_SP4)
        };
        bindViews(views, items);
    }

    class Item {
        String key;
        String customKey;

        public Item(KeyCodes key) {
            this.key = key.value() + "";
            if (emuMapKeysTemp != null)
                this.customKey = emuMapKeysTemp.get(this.key);
        }

        public Item(int key) {
            this.key = key + "";
            if (emuMapKeysTemp != null)
                this.customKey = emuMapKeysTemp.get(this.key);
        }

        public Item(String key, String customKey) {
            this.key = key;
            this.customKey = customKey;
        }

        public void reset() {
            if (emuMapKeysTemp != null)
                this.customKey = emuMapKeysTemp.get(this.key);
        }

        public boolean isBinded() {
            return TextUtils.isEmpty(customKey) ? false : true;
        }
    }

    void bindViews(View[] views, Item[] items) {
        for (int i = 0; views != null && i < views.length; i++) {
            if (views[i] == null) continue;
            views[i].setOnClickListener(this);

            items[i].reset();
            views[i].setTag(items[i]);
            selectCancel(views[i]);
        }
    }

    View selectView = null;

    String getCustomKeyName(String customKey) {
        if (TextUtils.isEmpty(customKey)) return "";
        return customKey.replace("KEY_", "").replace("BUTTON_", "");
    }

    void selectCancel(View view) {
        if (view == null || view.getTag() == null) return;
        Item item = (Item) view.getTag();
        if (view instanceof Button) {
            if (item.isBinded()) {
                view.setBackgroundResource(R.drawable.handshank_set_skey_pressed);
                ((Button) view).setTextColor(getResources().getColor(R.color.handshank_key_set_pressed_txt));
                String txt = getCustomKeyName(item.customKey);
                if (TextUtils.isEmpty(txt)) txt = "";
                ((Button) view).setText(txt);
            } else {
                view.setBackgroundResource(R.drawable.handshank_set_skey_normal);
                ((Button) view).setTextColor(getResources().getColor(R.color.handshank_key_set_normal_txt));
                ((Button) view).setText(getString(R.string.gamepad_key_undefined));
            }
        }
        selectView = null;
    }

    void select(View view) {
        if (view == null) return;
        if (selectView != null) {
            if (selectView.getId() == view.getId()) {
                selectCancel(selectView);
                return;
            }
            selectCancel(selectView);
        }
        selectView = view;
        if (view instanceof Button) {
            view.setBackgroundResource(R.drawable.handshank_set_skey_selected);
            ((Button) view).setTextColor(getResources().getColor(R.color.handshank_key_set_selected_txt));
        }
    }

    void bind(View view, String customKey) {
        if (view == null || view.getTag() == null) return;

        //移除之前绑定过此键值的按钮，并移除绑定
        View preBindedView = findPreBindedView(customKey);
        if (preBindedView != null) {
            bindCancel(preBindedView);
        }

        Item item = (Item) view.getTag();
        item.customKey = customKey;
        //数据
        emuMapKeysTemp.put(item.key, item.customKey);

        selectCancel(view);
    }

    void bindCancel(View view) {
        if (view == null || view.getTag() == null) return;
        Item item = (Item) view.getTag();
        item.customKey = "";

        //数据
        emuMapKeysTemp.put(item.key, item.customKey);

        selectCancel(view);
    }

    @Override
    public void onClick(View v) {
        Item item = (Item) v.getTag();

        select(v);
    }

    @Override
    protected boolean onKeyEvent(int keyCode, PadKeyEvent event) {
        String deviceName = event.getName();
        if (BlackListManager.getInstance().isInBlackList(deviceName)) return false;

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
//            onKeyDown1(keyCode, padEvent2KeyEvent(event));
        } else {
            onKeyUp1(keyCode, padEvent2KeyEvent(event));
        }

        return super.onKeyEvent(keyCode, event);
    }

    boolean isCustomKey(int key) {
        return customKeys != null ? customKeys.containsKey(key + "") : false;
    }

    public boolean onKeyUp1(int keyCode, KeyEvent event) {
        String deviceName = event.getDevice().getName();
        if (BlackListManager.getInstance().isInBlackList(deviceName)) return false;

        // TODO Auto-generated method stub
        if (keyCode != KeyEvent.KEYCODE_BACK) {

            //KEY_MAP里没有keycode值，不做处理，如：上下左右键等
            if (!isCustomKey(keyCode)) return true;
//
//            if (!KEY_MAP.containsValue(keyCode)) {
//                return true;
//            }

            if (selectView != null) {//有选中按钮
                String customKey = customKeys.get(keyCode + "");//即将要映射到的键值
                bind(selectView, customKey);

                isReset = false;
            }

//
//            //如果有选中的键，进入流程
//            if (selectBtn != null) {
//
//                //取出键上的tag值信息
//                String tag = (String) selectBtn.getTag();
//                //通过点击的keycode，获取keycode的名字
//                String keyBoradName = (String) HandShankUtil.valueGetKey(KEY_MAP, keyCode);
//                //如果这个keycode名字，在之前已经设过，并存入过map
//                if (okMap.containsKey(keyBoradName)) {
//                    //取出之前被选中的button的信息类
//                    KeyInfo keyInfo = data.get(okMap.get(keyBoradName));
//                    //把他的字置空
//                    ((TextView) findViewById(keyInfo.txt_id)).setText("");
//                }
//                //如果这个keycode名字，是被配置文件设置过
//                if (FBA_KEY_MAP.containsValue(keyBoradName)) {
//                    //取出这个默认设置button的tag值
//                    String value = (String) HandShankUtil.valueGetKey(FBA_KEY_MAP, keyBoradName);//Kmap2.get(keyBoradName);
//
//                    //取出这个默认设置button的info
//                    KeyInfo info = data.get(value);
//                    //把他的描述文字设为空
//                    ((TextView) findViewById(info.txt_id)).setText("");
//                }
//                //存入map中
//                okMap.put(keyBoradName, tag);
//                isReset = false;
//                KeyInfo info = data.get(tag);
//                selectBtn.setBackgroundResource(info.pressed_drawable);
//
//
//                //不再有选中的select
//                selectBtn = null;
//                //重置所有背景
//                revertBtn();


//            }

            return true;
        }
        return super.onKeyUp(keyCode, event);

    }

    private View findPreBindedView(String customKey) {
        View ret = null;
        for (int i = 0; views != null && i < views.length; i++) {
            Item item = (Item) views[i].getTag();
            if (item != null && !TextUtils.isEmpty(item.customKey) && item.customKey.equals(customKey)) {
                ret = views[i];
                break;
            }
        }
        return ret;
    }


//    private Button selectBtn;
//    private class KeyInfo {
//        public int id;
//        public int normal_drawable;
//        public int select_drawable;
//        public int pressed_drawable;
//        public int normal_txt_color;
//        public int select_txt_color;
//        public int pressed_txt_color;
//
//        public int txt_id;
//
//        public KeyInfo(int id, int normal_drawable, int select_drawable, int pressed_drawable, int normal_txt_color, int select_txt_color, int pressed_txt_color, int txt_id) {
//            this.id = id;
//            this.normal_drawable = normal_drawable;
//            this.select_drawable = select_drawable;
//            this.pressed_drawable = pressed_drawable;
//            this.normal_txt_color = normal_txt_color;
//            this.select_txt_color = select_txt_color;
//            this.pressed_txt_color = pressed_txt_color;
//            this.txt_id = txt_id;
//        }
//    }
//
//
//    private void initMapData() {
//
//        Map      map      = KeyMapManager.getMap("KEY");
//        Set      set      = map.keySet();
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()) {
//            String name = (String) iterator.next();
//            String key  = (String) map.get(name);
//            if (HandShankUtil.isAllowSet(name)) {
//                KEY_MAP.put(name, Integer.parseInt(key));
//            }
//        }
//
//        if (KEY_MAP == null || KEY_MAP.size() == 0) {
//            KEY_MAP.put(HandShankUtil.KEY_START, 108);
//            KEY_MAP.put(HandShankUtil.KEY_SELECT, 109);
//            KEY_MAP.put(HandShankUtil.KEY_X, 99);
//            KEY_MAP.put(HandShankUtil.KEY_A, 96);
//            KEY_MAP.put(HandShankUtil.KEY_Y, 100);
//            KEY_MAP.put(HandShankUtil.KEY_B, 97);
//            KEY_MAP.put(HandShankUtil.KEY_L1, 102);
//            KEY_MAP.put(HandShankUtil.KEY_L2, 104);
//            KEY_MAP.put(HandShankUtil.KEY_R1, 103);
//            KEY_MAP.put(HandShankUtil.KEY_R2, 105);
//            KEY_MAP.put(HandShankUtil.KEY_BUTTON_THUMBR, 107);
//        }
//
//
//        FBA_KEY_MAP = getMap();//KeyMapManager.getMap("FBA_KEY_MAP");
//
//        if (FBA_KEY_MAP == null || FBA_KEY_MAP.size() == 0) {
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_SELECT, HandShankUtil.KEY_SELECT);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_START, HandShankUtil.KEY_START);
//
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_C, HandShankUtil.KEY_X);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_A, HandShankUtil.KEY_A);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_D, HandShankUtil.KEY_Y);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_B, HandShankUtil.KEY_B);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_E, HandShankUtil.KEY_L1);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_F, HandShankUtil.KEY_L2);
//
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_AB, HandShankUtil.KEY_R1);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_CD, HandShankUtil.KEY_R2);
//            FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_ABC, HandShankUtil.KEY_BUTTON_THUMBR);
//        }
//
//
//    }


//    private Map<String, String> getMap() {
//        Map<String, String> ret = new HashMap<String, String>();
//
//        KeyMap.EmuMap       emuMap = KeyMap.EmuMap.FBA;
//        Map<String, String> map    = KeyMapManager.getMap(emuMap.section);
//
//        Iterator<Map.Entry<String, String>> iterator = HandShankUtil.fbaConvertMap.entrySet().iterator();
//        while (iterator.hasNext()) {
//            Map.Entry<String, String> entry = iterator.next();
//
//            String key   = entry.getKey();
//            String value = entry.getValue();
//
//            String V_KEY = map.get(value);
//
//            ret.put(key, V_KEY);
//
//        }
//
//        return ret;
//    }


//    private void reset() {
//        isReset = true;
//        okMap.clear();
//        okMap.put(HandShankUtil.KEY_SELECT, HandShankUtil.FBA_KEY_SELECT);
//        okMap.put(HandShankUtil.KEY_START, HandShankUtil.FBA_KEY_START);
//        okMap.put(HandShankUtil.KEY_X, HandShankUtil.FBA_KEY_C);
//        okMap.put(HandShankUtil.KEY_A, HandShankUtil.FBA_KEY_A);
//        okMap.put(HandShankUtil.KEY_Y, HandShankUtil.FBA_KEY_D);
//        okMap.put(HandShankUtil.KEY_B, HandShankUtil.FBA_KEY_B);
//        okMap.put(HandShankUtil.KEY_L1, HandShankUtil.FBA_KEY_E);
//        okMap.put(HandShankUtil.KEY_L2, HandShankUtil.FBA_KEY_F);
//
//        okMap.put(HandShankUtil.KEY_R1, HandShankUtil.FBA_KEY_AB);
//        okMap.put(HandShankUtil.KEY_R2, HandShankUtil.FBA_KEY_CD);
//        okMap.put(HandShankUtil.KEY_BUTTON_THUMBR, HandShankUtil.FBA_KEY_ABC);
//
//        FBA_KEY_MAP.clear();
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_SELECT, HandShankUtil.KEY_SELECT);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_START, HandShankUtil.KEY_START);
//
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_C, HandShankUtil.KEY_X);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_A, HandShankUtil.KEY_A);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_D, HandShankUtil.KEY_Y);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_B, HandShankUtil.KEY_B);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_E, HandShankUtil.KEY_L1);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_F, HandShankUtil.KEY_L2);
//
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_AB, HandShankUtil.KEY_R1);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_CD, HandShankUtil.KEY_R2);
//        FBA_KEY_MAP.put(HandShankUtil.FBA_KEY_ABC, HandShankUtil.KEY_BUTTON_THUMBR);
//
//
//        Set      set      = FBA_KEY_MAP.keySet();
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()) {
//            String keyname = (String) iterator.next();
//            String value   = FBA_KEY_MAP.get(keyname);
//
//            KeyInfo info = data.get(keyname);
//
//            ((TextView) findViewById(info.txt_id)).setText(HandShankUtil.nameToName.get(value));
//            (((Button) findViewById(info.id))).setBackgroundResource(info.normal_drawable);
//            if (info.normal_txt_color != 0) {
//                (((Button) findViewById(info.id))).setTextColor(getResources().getColor(info.normal_txt_color));
//            }
//
//        }
//
//
//        KeyMapManager.writeToConfig(this, KeyMap.EmuMap.FBA, putMap(okMap));
//        okMap.clear();
//
//    }
//
//    private void initView1() {
//
//
//        Set      set      = FBA_KEY_MAP.keySet();
//        Iterator iterator = set.iterator();
//        while (iterator.hasNext()) {
//            String  keyname = (String) iterator.next();
//            String  value   = FBA_KEY_MAP.get(keyname);
//            KeyInfo info    = data.get(keyname);
//
//            if (KEY_MAP.containsKey(value)) {
//                System.out.println("value-------------" + value);
//                System.out.println("keyname-------------" + keyname);
//                ((TextView) findViewById(info.txt_id)).setText(HandShankUtil.nameToName.get(value));
//            } else {
//                ((TextView) findViewById(info.txt_id)).setText("");
//            }
//
//
//        }
//
//    }
//
//    private void initMap() {
//        data = new HashMap<String, KeyInfo>();
//        data.put(HandShankUtil.FBA_KEY_START, new KeyInfo(R.id.start, R.drawable.handshank_set_skey_normal, R.drawable.handshank_set_skey_selected, R.drawable.handshank_set_skey_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.start_tv));
//        data.put(HandShankUtil.FBA_KEY_SELECT, new KeyInfo(R.id.select, R.drawable.handshank_set_skey_normal, R.drawable.handshank_set_skey_selected, R.drawable.handshank_set_skey_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.select_tv));
//
//        data.put(HandShankUtil.FBA_KEY_A, new KeyInfo(R.id.a, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.a_tv));
//        data.put(HandShankUtil.FBA_KEY_B, new KeyInfo(R.id.b, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.b_tv));
//
//        data.put(HandShankUtil.FBA_KEY_C, new KeyInfo(R.id.c, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.c_tv));
//        data.put(HandShankUtil.FBA_KEY_D, new KeyInfo(R.id.d, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.d_tv));
//
//        data.put(HandShankUtil.FBA_KEY_E, new KeyInfo(R.id.e, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.l1_tv));
//
//
//        data.put(HandShankUtil.FBA_KEY_F, new KeyInfo(R.id.f, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.l2_tv));
//        data.put(HandShankUtil.FBA_KEY_AB, new KeyInfo(R.id.ab, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.r1_tv));
//        data.put(HandShankUtil.FBA_KEY_CD, new KeyInfo(R.id.cd, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.r2_tv));
//        data.put(HandShankUtil.FBA_KEY_ABC, new KeyInfo(R.id.abc, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt, R.id.add_tv));
//
//        initMapData();
//    }
//
//    private void initBtn() {
//
//        select.setTag("FBA_KEY_SELECT");
//        start.setTag("FBA_KEY_START");
//        select.setText("投币");
//        start.setText("开始");
//        select.setOnClickListener(this);
//        start.setOnClickListener(this);
//
//        a.setOnClickListener(this);
//
//        b.setOnClickListener(this);
//
//        c.setOnClickListener(this);
//
//        d.setOnClickListener(this);
//
//        e.setOnClickListener(this);
//
//        f.setOnClickListener(this);
//
//        ab.setOnClickListener(this);
//
//        cd.setOnClickListener(this);
//
//        abc.setOnClickListener(this);
//    }
//
//
//    public void onClick1(View view) {
//        String tag = (String) view.getTag();
//
//        //检查这个按钮是不是在已经设置完毕的map里
//        if (!okMap.containsValue(tag)) {//不在已经设置的map里
//            //已经有设置过的selectBrn
//            if (selectBtn != null) {
//                //如果是相同的按钮，不做处理
//                if (tag.equals(selectBtn.getTag())) {
//                    return;
//                }
//                //如果不是相同的按钮，取出select上的keyinfo，该对象包含按钮的文字背景信息
//                KeyInfo keyInfo = data.get(selectBtn.getTag());
//                //将按钮的背景设置为未点击
//                selectBtn.setBackgroundResource(keyInfo.normal_drawable);
//                //如果按钮文字可设置
//                if (keyInfo.normal_txt_color != 0) {
//                    //将按钮文字设为未点击
//                    selectBtn.setTextColor(getResources().getColor(R.color.handshank_key_set_normal_txt));
//                }
//            }
//            //给select赋新值，新值为当前点击的button
//            selectBtn = (Button) view;
//            //取出btn上的tag，获取按钮信息keyInfo对像
//            KeyInfo keyInfo = data.get(selectBtn.getTag());
//            //设置按钮的背景及文字
//            selectBtn.setBackgroundResource(keyInfo.select_drawable);
//            if (keyInfo.normal_txt_color != 0) {
//                selectBtn.setTextColor(getResources().getColor(R.color.handshank_key_set_selected_txt));
//            }
//
//        }
//
//
//    }
//
//    private void revertBtn1() {
//
//        Set      set = data.entrySet();
//        Iterator i   = set.iterator();
//        while (i.hasNext()) {
//            Map.Entry<String, KeyInfo> entry1 = (Map.Entry<String, KeyInfo>) i.next();
//            String                     key    = entry1.getKey();
//            KeyInfo                    info   = data.get(key);
//            //取出当前所有的按钮并且判断信息
//            //如果这个button已被加入okMap
//
//            if (okMap.containsValue(key)) {
//                //取出这个button，背景文字设成被选中
//                Button button = (Button) findViewById(info.id);
//
//                if (info.normal_txt_color != 0) {
//                    button.setTextColor(getResources().getColor(info.pressed_txt_color));
//                }
//
//                button.setBackgroundResource(info.pressed_drawable);
//
//                //将文本设置成键名
//                ((TextView) findViewById(info.txt_id)).setText(HandShankUtil.nameToName.get(HandShankUtil.valueGetKey(okMap, key)));
//
//            } else {
//                //如果这个button没被加入okMap，设置成正常的
//                Button button = (Button) findViewById(info.id);
//                if (info.normal_txt_color != 0) {
//                    button.setTextColor(getResources().getColor(info.normal_txt_color));
//                }
//                button.setBackgroundResource(info.normal_drawable);
//
//            }
//
//        }
//
//    }

//    private List<String> unSetKeyBoradName;
//    private List<String> unSetTag;
//
//
//    //最终数据包
//    private Map<String, String> okMap = new HashMap<String, String>();

//
//    //手柄键盘映射
//    private Map<String, Integer> KEY_MAP = new HashMap<String, Integer>();
//
//    //手柄键盘映射
//    private Map<String, String> FBA_KEY_MAP = new HashMap<String, String>();

//    //按钮类映射
//    private Map<String, KeyInfo> data;
//
//    private void unSet() {
//        unSetKeyBoradName = new ArrayList<String>();
//        unSetTag = new ArrayList<String>();
//        for (String tag : FBA_KEY_MAP.keySet()) {
//            if (okMap.containsValue(tag)) {
//                continue;
//            }
//            unSetTag.add(tag);
//        }
//
//        for (String keyBoradName : FBA_KEY_MAP.values()) {
//            if (okMap.containsKey(keyBoradName)) {
//                continue;
//            }
//            unSetKeyBoradName.add(keyBoradName);
//        }
//
//        Iterator iterator = unSetTag.iterator();
//        while (iterator.hasNext()) {
//            String tag          = (String) iterator.next();
//            String keyBoradName = FBA_KEY_MAP.get(tag);
//            if (unSetKeyBoradName.contains(keyBoradName)) {
//                iterator.remove();
//                unSetKeyBoradName.remove(keyBoradName);
//
//                okMap.put(keyBoradName, tag);
//            }
//        }
//
//        if (unSetKeyBoradName.size() >= unSetTag.size()) {
//            for (int i = 0; i < unSetTag.size(); i++) {
//                //okMap.put(unSetKeyBoradName.get(i),unSetTag.get(i));
//                okMap.put(unSetKeyBoradName.get(i), unSetTag.get(i));
//            }
//        }
//
//    }


//    private Map<String, String> putMap(Map<String, String> okMap) {
//        Map<String, String> ret = new HashMap<String, String>();
//
//        Iterator iterator = okMap.keySet().iterator();
//        while (iterator.hasNext()) {
//            String key   = (String) iterator.next();
//            String value = okMap.get(key);
//
//            System.out.println("key-------------" + key);
//            System.out.println("value-------------" + value);
//
//            ret.put(key, HandShankUtil.fbaConvertMap.get(value));
//
//        }
//
//
//        return ret;
//    }

}
